var express = require('express');
var router = express.Router();

// 导入用户表
var user = require("../bin/UserDao.js");
// 配置session
var session = require("express-session");
router.use(session({
  secret:"keyboard cat",
  resave:false,
  saveUninitialized:true,
  cookie:{

  }
}));
/* GET home page. */
router.get('/Home', function(req, res) {
  var imgs=[
      {img:"/images/1.jpg"},
      {img:"/images/2.jpg"},
      {img:"/images/3.jpg"},
      {img:"/images/4.jpg"},
      {img:"/images/5.jpg"},
  ]

  var imgs2 = [
    {img:"/images/2.1.png"},
    {img:"/images/2.2.png"},
  ]

  var imgs3 = [
    {img:"/images/3.1.jpg"},
    {img:"/images/3.2.jpg"},
    {img:"/images/3.3.jpg"},
    {img:"/images/3.4.jpg"},
    {img:"/images/3.5.jpg"},
    {img:"/images/3.6.jpg"},
    {img:"/images/3.7.jpg"},
    {img:"/images/3.8.jpg"},
  ]

  var imgs5 = [
    {img:"/images/4.1.jpg",title:"芸茗普洱茶 刮风寨 2013年 生茶 357g、饼",content:"饭后常喝普洱不油腻",price:"680.00",imgs:[
      {img:'/images/4.1.1.jpg'},
      {img:'/images/4.1.2.jpg'},
      {img:'/images/4.1.3.jpg'},
      {img:'/images/4.1.4.jpg'},
      {img:'/images/4.1.5.jpg'},
      {img:'/images/4.1.6.jpg'},
      {img:'/images/4.1.jpg'},
    ]},
    {img:"/images/4.2.jpg",title:"芸茗普洱茶 弯弓 2013年 生茶 357g、饼",content:"易武甜茶代表 入口淡雅甜柔 初尝普洱推荐",price:"520.00",imgs:[
      {img:'/images/4.2.jpg'},
      {img:'/images/4.2.1.jpg'},
      {img:'/images/4.2.2.jpg'},
      {img:'/images/4.2.3.jpg'},
      {img:'/images/4.2.4.jpg'},
    ]},
    {img:"/images/4.3.jpg",title:"芸茗普洱茶 弯弓 2012年 熟茶 357g、饼",content:"5年陈 醇和口粮茶 自饮收藏",price:"520.00",imgs:[
      {img:'/images/4.3.jpg'},
      {img:'/images/4.3.1.jpg'},
      {img:'/images/4.3.2.jpg'},
      {img:'/images/4.3.3.jpg'},
      {img:'/images/4.3.4.jpg'},
      {img:'/images/4.3.5.jpg'},
      
    ]},
    {img:"/images/4.4.jpg",title:"[老茶] 高家山安化黑茶 2011年 特级 陈年野生千两茶 方块直泡 300g 黄金礼盒",content:"金箔纸颗粒装 比传统砖茶更易冲泡",price:"503.00",imgs:[
      {img:'/images/4.4.jpg'},
      {img:'/images/4.4.1.jpg'},
      {img:'/images/4.4.2.jpg'},
      {img:'/images/4.4.3.jpg'},
      {img:'/images/4.4.4.jpg'},
      {img:'/images/4.4.5.jpg'},
      {img:'/images/4.4.6.jpg'},
      
    ]},
    {img:"/images/4.5.jpg",title:"高家山安化黑茶 2014年 特级 湘野一号 500g 礼盒",content:"饭后常喝普洱不油腻",price:"836.00",imgs:[
      {img:'/images/4.5.jpg'},
      {img:'/images/4.5.1.jpg'},
      {img:'/images/4.5.2.jpg'},
      {img:'/images/4.5.3.jpg'},
      {img:'/images/4.5.4.jpg'},
      {img:'/images/4.5.5.jpg'},
      {img:'/images/4.5.6.jpg'},
      
    ]},
  ]  
  req.session.imgs5 = imgs5;      
  res.json({imgs,imgs2,imgs3,imgs5})
});

// 注册
router.get("/regist",function(req,res){
    user.find({username:req.query.username})
    .exec(function(err,data){
      if(data.length>0){
        res.json({err:1,msg:"此用户已被占用"})
      }else{
        var u = new user(req.query);
        u.save(function(err){
          res.json({err:0,msg:"注册成功，请登录"})
        })
      }
    })
})

// 登录
router.get("/login",function(req,res){
  user.findOne({username:req.query.username})
  .exec(function(err,data){
    if(!data){
      res.json({err:1,msg:"没有此用户"})
    }else{
      if(req.query.psw==data.psw){
        req.session.user = data;
        res.json({err:0,msg:"登录成功",data});
      }else{
        res.json({err:1,msg:"密码错误"});
      }
    }
  })
})

// 退出登录
router.get("/tuichu",function(req,res){
  req.session.user = null;
  res.json({err:0,msg:"退出成功"});
})

// 分类
router.get("/classify",function(req,res){
  var classify = [
    {type:"乌龙茶",content:[
      {type:"安溪提观音",img:"/images/安溪铁观音.jpg",List:[
        {img:"/images/4.1.jpg",title:"芸茗普洱茶 刮风寨 2013年 生茶 357g、饼",content:"饭后常喝普洱不油腻",price:"680.00",year:"2013",weight:"357",level:"一级"},
        {img:"/images/4.2.jpg",title:"芸茗普洱茶 弯弓 2013年 生茶 357g、饼",content:"易武甜茶代表 入口淡雅甜柔 初尝普洱推荐",price:"520.00",year:"2013",weight:"357",level:"一级"},
        {img:"/images/4.3.jpg",title:"芸茗普洱茶 弯弓 2012年 熟茶 357g、饼",content:"5年陈 醇和口粮茶 自饮收藏",price:"520.00",year:"2012",weight:"357",level:"二级"},
        {img:"/images/4.4.jpg",title:"[老茶] 高家山安化黑茶 2011年 特级 陈年野生千两茶 方块直泡 300g 黄金礼盒",content:"金箔纸颗粒装 比传统砖茶更易冲泡",price:"503.00",year:"2011",weight:"300",level:"一级"},
        {img:"/images/4.5.jpg",title:"高家山安化黑茶 2014年 特级 湘野一号 500g 礼盒",content:"饭后常喝普洱不油腻",price:"836.00",year:"2014",weight:"500",level:"特级"},
      ]},
      {type:"武夷岩茶",img:"/images/武夷大红袍.jpg",List:[

      ]},
      {type:"台湾高山茶",img:"/images/台湾高山茶.jpg",List:[

      ]},
      {type:"广东单枞",img:"/images/广东单从.jpg",List:[

      ]},
      {type:"漳平水仙",img:"/images/漳平水仙.jpg",List:[

      ]},
      {type:"其他乌龙茶",img:"/images/其他乌龙茶.jpg",List:[

      ]},
    ]},
    {type:"红茶",content:[
      {type:"金骏眉",img:"/images/金骏眉.jpg",List:[

      ]},
      {type:"正山小种",img:"/images/安溪铁观音.jpg",List:[

      ]},
      {type:"祁门红茶",img:"/images/安溪铁观音.jpg",List:[

      ]},
      {type:"云南滇红",img:"/images/金骏眉.jpg",List:[

      ]},
      {type:"英德红茶",img:"/images/英德红茶.jpg",List:[

      ]},
      {type:"锡兰红茶",img:"/images/锡兰红茶.jpg",List:[

      ]},
      {type:"九曲红梅",img:"/images/九曲红梅.jpg",List:[

      ]},
      {type:"其他绿茶",img:"/images/九曲红梅.jpg",List:[

      ]},
    
    

    ]},
    {type:"绿茶",content:[
      {type:"龙井",img:"/images/龙井.jpg",List:[

      ]},
      {type:"碧螺春",img:"/images/碧螺春.jpg",List:[

      ]},
      {type:"黄山毛峰",img:"/images/黄山毛峰.jpg",List:[

      ]},
      {type:"六安瓜片",img:"/images/六安瓜片.jpg",List:[

      ]},
      {type:"太平猴魁",img:"/images/太平猴魁.jpg",List:[

      ]},
      {type:"安吉白茶",img:"/images/安吉白茶.jpg",List:[

      ]},
      {type:"云南滇绿",img:"/images/云南滇绿.jpg",List:[

      ]},
      {type:"四川绿茶",img:"/images/四川绿茶.jpg",List:[

      ]},

    ]},
    {type:"黑茶",content:[
      {type:"普洱茶",img:"/images/普洱生茶.jpg",list:[

      ]},
      {type:"安化黑茶",img:"/images/安化黑茶.jpg",list:[

      ]},
    ]}

  ]
  req.session.classify = classify;
  res.json({classify})
})

router.get("/goodslist",function(req,res){
  var data = req.session.classify;
  var data1 = data.filter(function(i){
    return i.type == req.query.type;
  });
  var data2 = data1[0].content
  var data3 = data2.filter(function(j){
    return j.type == req.query.title;
  });
  res.json({data3})

})


// 承担购物车功能
var goodsCard = [];
// 个人商品集合
// var goods = {
//   username:"",
//   commodits:[]
// };
var commodit = {};

// 商品详情
router.get("/goodsDetails",function(req,res){
  var data = req.session.imgs5;
  var data1 = data.filter(function(i){
    return i.title == req.query.title;
  })
  commodit = data1[0];
  res.json({data1});
})



// 加入购物车
router.get("/joinGoodsCard",function(req,res){
  commodit.username = req.session.user.username;
  commodit.number = req.query.number;
  goodsCard.push(commodit);
  res.json({err:0,msg:"添加成功"});

})
var data;
// 购物车列表
router.get("/GoodsCard",function(req,res){
  // console.dir(goodsCard)
  var data1 = goodsCard.filter(function(i){
    return i.username == req.query.username;
  });
  var data2 = data1.filter(function(j){
    return j.check = false;
  })
  data = data1;
  res.json({data1})
})


// 购物车减数量
router.get("/reducenumber",function(req,res){
  var data2 = data.filter(function(i){
    return i.title == req.query.title;
  })

  data2[0].number--;
  res.json({msg:"减少成功"})
})

// 购物车加数量
router.get("/addnumber",function(req,res){
  var data2 = data.filter(function(i){
    return i.title == req.query.title;
  })
  data2[0].number++;
  res.json({msg:"增加成功"})
})

// 删除购物车商品
router.get("/delpicture",function(req,res){
  var data2 = goodsCard.filter(function(i){
    return i.title == req.query.title;
  });
  var d= goodsCard.indexOf(data2[0]);
  goodsCard.splice(d,1)
  res.json({msg:"删除成功"})
})
module.exports = router;
